﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Share_Insert_UpdateSales : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {    
            LoadData();
           
  
    }
   

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("MaintainSale.aspx");
    }

    public void LoadData()
    {
        SaleDAO saleDAO = new SaleDAO();
        int saleID = int.Parse(Request["SalesID"].ToString());
        dtlSalePosting.DataSource = saleDAO.GetSaleDetails(saleID);
        dtlSalePosting.DataBind();
        CheckFill();
    }

    /*
    public void onCheckedChanged(object sender, EventArgs e)
    {
        foreach (DataListItem item in dtlSalePosting.Items)
        {
            if (((RadioButton)item.FindControl("radSelling")).Checked == true)
            {
                ((Label)item.FindControl("lblPaid")).Visible = false;
                ((RadioButton)item.FindControl("radPaid")).Visible = false;
                ((RadioButton)item.FindControl("radNotPaid")).Visible = false;
            }
             if (((RadioButton)item.FindControl("radSold")).Checked == true)
            {
                ((Label)item.FindControl("lblPaid")).Visible = true;
                ((RadioButton)item.FindControl("radPaid")).Visible = true;
                ((RadioButton)item.FindControl("radNotPaid")).Visible = true;
            }
        }
    }
     * */

   
    /*
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        SaleDAO saleDAO = new SaleDAO();
        int saleID = int.Parse(Request["SalesID"].ToString());
        foreach (DataListItem item in dtlSalePosting.Items)
        {
            if (((RadioButton)item.FindControl("radSelling")).Checked == true)
            {
                if (saleDAO.Delete(saleID) > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
                 //   Response.Redirect("MaintainSale.aspx");
                    LoadData();
                }
                else
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Failed!');", true);
            }
            else if (((RadioButton)item.FindControl("radPaid")).Checked == true)
            {
                SaleInfo saleInfo = new SaleInfo();
                saleInfo.SalesID = saleID;
                if (saleDAO.UpdatePaid(saleInfo) > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
                //    Response.Redirect("MaintainSale.aspx");
                    LoadData();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
                }

            }
        }
    }
     * */

    void CheckFill()
    {
        try
        {
            foreach (DataListItem item in dtlSalePosting.Items)
            {
                if (((Label)item.FindControl("lblInvi")).Text.Equals("True"))
                {
                    ((RadioButton)item.FindControl("radPaid")).Checked = true;
                }
                else if (((Label)item.FindControl("lblInvi")).Text.Equals("False"))
                {
                    ((RadioButton)item.FindControl("radNotPaid")).Checked = true;
                }

                if (((Label)item.FindControl("lblSta")).Text.Equals("Selling"))
                {
                    ((RadioButton)item.FindControl("radSelling")).Checked = true;
                }
                else if (((Label)item.FindControl("lblSta")).Text.Equals("Sold"))
                {
                    ((RadioButton)item.FindControl("radSold")).Checked = true;
                }


            }
        }
        catch (Exception ex) { }
    }

    protected void dtlSalePosting_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        SaleDAO saleDAO = new SaleDAO();
        PostingExhibitionDAO postExhiDAO = new PostingExhibitionDAO();
        int saleID = int.Parse(Request["SalesID"].ToString());

        SaleInfo saleInfo = new SaleInfo();
        saleInfo.SalesID = saleID;
        SaleInfo sale = new SaleInfo();
        
        sale = saleDAO.GetByID(saleID);


        PostingExhibitionInfo postInfo = new PostingExhibitionInfo();
        postInfo.PostingID = sale.PostingID;

        if (((RadioButton)e.Item.FindControl("radSelling")).Checked)
            postInfo.Status = "Selling";
        else if (((RadioButton)e.Item.FindControl("radSold")).Checked)
            postInfo.Status = "Sold";

        if (((RadioButton)e.Item.FindControl("radPaid")).Checked == true)
            saleInfo.Paid = true;
        else if (((RadioButton)e.Item.FindControl("radNotPaid")).Checked == true)
            saleInfo.Paid = false;

       
        if (saleDAO.UpdatePaid(saleInfo) > 0 && postExhiDAO.UpdateStatus(postInfo) > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
            LoadData();
        }

        /*

            if (((RadioButton)e.Item.FindControl("radSelling")).Checked == true)
            {
                if (saleDAO.Delete(saleID) > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
                    Response.Redirect("MaintainSale.aspx");
                    LoadData();
                }
                else
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Failed!');", true);
            }
            else if (((RadioButton)e.Item.FindControl("radPaid")).Checked == true)
            {
       
                saleInfo.SalesID = saleID;
                if (saleDAO.UpdatePaid(saleInfo) > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
                    //    Response.Redirect("MaintainSale.aspx");
                    LoadData();
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
                }

            }
        */


    }
}